package com.ziyun.pop.mall.module.user.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.service.common.protocol.ResultCode;
import com.service.goods.api.SubSellerService;
import com.service.goods.constant.GoodsResult;
import com.service.goods.model.seller.SellerInfoVO;
import com.service.goods.model.seller.SellerListResultDto;
import com.service.goods.model.seller.SubSellerListDto;
import com.service.soa.service.impl.BaseServiceImpl;
import com.service.ucenter.api.VerifyCodeService;
import com.service.ucenter.exception.UserException;
import com.ziyun.pop.mall.system.config.HttpRequestLocal;

/**
 * 在主店下增加分店
 * 
 * @author husong 9.26
 *
 */
@Service
@Transactional
public class UserAddMerchantService extends BaseServiceImpl {

	@Autowired
	private SubSellerService subSellerService;

	@Autowired
	private VerifyCodeService verifyCodeService;

	@Autowired
	private HttpRequestLocal httpRequestLocal;

	/**
	 * 获取登录账号的商家信息
	 * 
	 * @return
	 */
	public GoodsResult<SellerInfoVO> getMainSellerInfoVO() {
		// 主店的信息
		long sellerId = getSellerId();
		GoodsResult<SellerInfoVO> sellerDetails = subSellerService.getSellerDetails(sellerId, "true");
		SellerInfoVO sellerInfo = sellerDetails.getData();
		return GoodsResult.success(sellerInfo);
	}

	/**
	 * 获取商家的分店的列表
	 * 
	 * @return
	 */
	public GoodsResult<SellerListResultDto> getSubSellerList(SubSellerListDto sellerInfoParam) {
		sellerInfoParam.setMainSellerId(getUserId());
		return subSellerService.getSubSellerList(sellerInfoParam);
	}

	/**
	 * 增加分店
	 * 
	 * @param subSellerInfoVO
	 *            修改后分店信息
	 * @return
	 */
	public GoodsResult addMerchantService(SellerInfoVO subSellerInfoVO, String verifyCode) {

		String subSellerMobile = subSellerInfoVO.getSellerMobile(); // 手机号码
		// TODO 验证验证码是否正确
		verifyCodeService.checkVerifyCode(subSellerMobile, verifyCode);
		subSellerInfoVO.setMainSellerId(getSellerId());
		// 新增店铺
		return subSellerService.addsubMerchant(subSellerInfoVO);
	}

	/**
	 * 获取sellerId
	 * 
	 * @return
	 */
	public long getSellerId() {
		Object seller = httpRequestLocal.getSessionValue("sellerId");
		if (StringUtils.isEmpty(seller)) {
			throw new UserException(ResultCode.MERCHANTS_NON_EXISTENT);// 商户不存在
		}
		long sellerId = Long.valueOf(String.valueOf(seller)).longValue();
		return sellerId;
	}

}
